﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace MTGScore
{
    public enum DiceType
    {
        Type4,
        Type6,
        Type8,
        Type10,
        Type12,
        Type20,
        Type100
    }
    public partial class DicePage : PhoneApplicationPage
    {
        DiceType diceType1 = DiceType.Type20;
        DiceType diceType2 = DiceType.Type20;
        DiceType diceTypeAll = DiceType.Type20;
        Random random = new Random();
        
        public DicePage()
        {
            InitializeComponent();
            Result_1.Text =""+ GetDiceResult(diceType1);
            Result_2.Text = ""+GetDiceResult(diceType2);
        }
        private void DicePage_Loaded(object sender, RoutedEventArgs e)
        {

            
            
        }
        
        int GetDiceResult(DiceType type)
        {
            int r = random.Next(10000);
            switch (type)
            {
                case DiceType.Type4:
                    
                    return r%4 + 1;
                case DiceType.Type6:

                    return r%6+1;
                case DiceType.Type8:

                    return r%8+1;
                case DiceType.Type10:

                    return r%10;
                case DiceType.Type12:

                    return r%12+1;
                case DiceType.Type20:

                    return r%20+1;
                case DiceType.Type100:

                    return (r%10)*10;
            }


            return 0;
        }

        private void Dice_1_Click(object sender, RoutedEventArgs e)
        {
            Result_1.Text = "" + GetDiceResult(diceType1);
        }

        private void Dice_2_Click(object sender, RoutedEventArgs e)
        {
            Result_2.Text = "" + GetDiceResult(diceType2);
        }

        private void Dice_All_Click(object sender, RoutedEventArgs e)
        {
            Result_1.Text = "" + GetDiceResult(diceTypeAll);
            Result_2.Text = "" + GetDiceResult(diceTypeAll);
        }
        private DiceType GetDiceType(object sender, DiceType prev)
        {
            try
            {
                var t = (MenuItem)sender;
                String s = (String)t.Header;

                if (s == GetDiceTypeString(DiceType.Type4))
                    return DiceType.Type4;
                if (s == GetDiceTypeString(DiceType.Type6))
                    return DiceType.Type6;
                if (s == GetDiceTypeString(DiceType.Type8))
                    return DiceType.Type8;
                if (s == GetDiceTypeString(DiceType.Type10))
                    return DiceType.Type10;
                if (s == GetDiceTypeString(DiceType.Type12))
                    return DiceType.Type12;
                if (s == GetDiceTypeString(DiceType.Type20))
                    return DiceType.Type20;
                if (s == GetDiceTypeString(DiceType.Type100))
                    return DiceType.Type100;
                return prev;
            }
            catch (Exception ex)
            {
                ex.ToString();
                return prev;
            }
        }
        string GetDiceTypeString(DiceType type)
        {
            if (type == DiceType.Type4)
                return "4";
            if (type == DiceType.Type6)
                return "6";
            if (type == DiceType.Type8)
                return "8";
            if (type == DiceType.Type10)
                return "10";
            if (type == DiceType.Type12)
                return "12";
            if (type == DiceType.Type20)
                return "20";
            if (type == DiceType.Type100)
                return "100";
            return "6";
        }
        private void Type1_Click(object sender, RoutedEventArgs e)
        {
            diceType1 = GetDiceType(sender, diceType1);
            DiceTypeText1.Text = GetDiceTypeString(diceType1);
        }
        private void Type2_Click(object sender, RoutedEventArgs e)
        {
            diceType2 = GetDiceType(sender, diceType2);
            DiceTypeText2.Text = GetDiceTypeString(diceType2);
        }
        private void TypeAll_Click(object sender, RoutedEventArgs e)
        {
            diceTypeAll = GetDiceType(sender, diceTypeAll);
            DiceTypeTextAll.Text = GetDiceTypeString(diceTypeAll);

            diceType1 = GetDiceType(sender, diceTypeAll);
            DiceTypeText1.Text = GetDiceTypeString(diceType1);

            diceType2 = GetDiceType(sender, diceTypeAll);
            DiceTypeText2.Text = GetDiceTypeString(diceType2);
        }
        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            try
            {
                Border border = sender as Border;
                ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
                if (contextMenu.Parent == null)
                {
                    contextMenu.IsOpen = true;
                }
            }
            catch (Exception)
            { }
        }

        /*
        private void TypeChooserText_2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorder2,null);
        }
        private void TypeChooserText_1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorder1, null);
        }
        private void TypeChooserText_All_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GestureListener_Tap(DiceBorderAll, null);
        }*/

        private void TapDice1_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorder1, null);
        }
        private void TapDice2_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorder2, null);
        }
        private void TapDiceAll_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            GestureListener_Tap(DiceBorderAll, null);
        }

        private void AdControl_AdControlError(object sender, Microsoft.Advertising.Mobile.UI.ErrorEventArgs e)
        {

        }

        
    }
}